package com.mjt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mjt.admin.dto.TreeDto;
import com.mjt.admin.mapper.RoleMenuMapper;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Menu;
import com.mjt.admin.mapper.MenuMapper;
import com.mjt.admin.pojo.RoleMenu;
import com.mjt.admin.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjt.admin.service.IRoleMenuService;
import com.mjt.admin.utils.AssertUtil;
import com.mjt.admin.utils.PageResultUtil;
import com.mjt.admin.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author 莫某人
 * @since 2021-10-27
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    /**
     * @description 查询所有菜单
     * @author 莫某人
     * @date 11:09 2021/10/29
     * @return {@link }
     **/

    @Resource
    private MenuMapper menuMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<TreeDto> queryAllMenus(Integer roleId) {
        List<TreeDto> treeDtos = menuMapper.queryAllMenus();
        //查询角色拥有的权限
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>()
                .eq("role_id", roleId)
        );
        List<Integer> menus = roleMenus.stream().map((r) -> {
            return r.getMenuId();
        }).collect(Collectors.toList());

        List<TreeDto> dtoList = treeDtos.stream().map((t) -> {
            if (menus.contains(t.getId())){
                t.setChecked(true);
                return t;
            }else {
                return t;
            }
        }).collect(Collectors.toList());
        return dtoList;
    }



    /**
     * @description 获取菜单列表
     * @author 莫某人
     * @date 9:48 2021/11/1
     * @return {@link RespDataBean< Menu>}
     **/
    @Override
    public Map<String, Object> menuList() {
        List<Menu> menuList = this.list(new QueryWrapper<Menu>().eq("is_del", 0));
        return PageResultUtil.getResult((long) menuList.size(),menuList);
    }

    /**
     * @description 添加菜单
     * @author 莫某人
     * @date 10:28 2021/11/1
     * @param menu
     * @return {@link }
     **/
    @Override
    public void saveMenu(Menu menu) {
        AssertUtil.isTrue(StringUtil.isEmpty(menu.getName()),"菜单名不能为空");
        AssertUtil.isTrue(StringUtil.isEmpty(menu.getAclValue()),"权限码不能为空");
        AssertUtil.isTrue(!this.save(menu),"添加失败");
    }

    @Override
    public void delete(Integer id) {
        //判断是否含有子菜单
        List<Menu> list = this.list(new QueryWrapper<Menu>().eq("p_id", id));

        AssertUtil.isTrue(list.size()>0,"无法删除，请先删除子菜单");

        this.removeById(id);


    }

    @Override
    public void updateMenu(Menu menu) {
        //查询


        AssertUtil.isTrue(menu.getPId()==null,"pid不能为空");

        this.updateById(menu);


    }


}
